from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from models import User
from schemas import UserCreate, UserOut, UserUpdate, UserStatusUpdate, UserLogin, TokenOut
from services import (
    create_user, get_user, update_user, delete_user, list_users, update_user_status
)
from dao import get_db  # 改为绝对导入，适配直接运行

router = APIRouter()

@router.post("/users", response_model=UserOut)
def create_user_api(user: UserCreate, db: Session = Depends(get_db)):
    return create_user(user, db)

@router.get("/users/{id}", response_model=UserOut)
def get_user_api(id: int, db: Session = Depends(get_db)):
    return get_user(id, db)

@router.put("/users/{id}", response_model=UserOut)
def update_user_api(id: int, user: UserUpdate, db: Session = Depends(get_db)):
    return update_user(id, user, db)

@router.delete("/users/{id}")
def delete_user_api(id: int, db: Session = Depends(get_db)):
    delete_user(id, db)
    return {"msg": "用户已删除"}

@router.get("/users", response_model=List[UserOut])
def list_users_api(page: int = 1, page_size: int = 10, db: Session = Depends(get_db)):
    return list_users(page, page_size, db)["items"]

@router.put("/users/{id}/status", response_model=UserOut)
def update_user_status_api(id: int, status: UserStatusUpdate, db: Session = Depends(get_db)):
    return update_user_status(id, status, db)

# 其他接口（如登录、角色、权限管理）可按需补充 